package com.kpmg.datalake.common.utils;

import com.kpmg.datalake.common.constants.WebConstants;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * cookie相关操作工具类
 * @author sail
 */
public class CookieUtils {
	
	private CookieUtils() {
		
	}
	
	/**
	 * 记住用户
	 */
	public static void userRemember(boolean isRemember, String username, HttpServletRequest request,
			HttpServletResponse response) {

		// one month
		int maxAge = 30 * 24 * 60 * 60;


		if (isRemember) {
			Cookie userNameCookie = new Cookie(WebConstants.SESSION_USER_NAME, username);
			userNameCookie.setPath("/");
			userNameCookie.setMaxAge(maxAge);

			response.addCookie(userNameCookie);
		} else {
			Cookie[] cookies = request.getCookies();
			if (cookies != null) {
				for (Cookie cookie : cookies) {
					if (WebConstants.SESSION_USER_NAME.equals(cookie.getName())) {
						cookie.setMaxAge(0);
						cookie.setPath("/");
						response.addCookie(cookie);
					}
				}
			}
		}
	}
}
